package asedra.huyle.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import asedra.huyle.dto.Match;
import asedra.huyle.dto.Player;
import asedra.huyle.dto.Record;

public class PlayerDao extends BaseDao<Match>{

	public PlayerDao(Context context, String databaseName) {
		super(context, databaseName);
	}

	public PlayerDao(SQLiteDatabase db) {
		super(db);
	}

	public PlayerDao(SQLiteOpenHelper helper) {
		super(helper);
	}
	
	public long insert(Player player) {
        ContentValues values = new ContentValues();
        values.put(Player.PLAYER_ID, player.getPlayerId());
        values.put(Player.NAME, player.getName());
        values.put(Player.AVATAR, player.getAvatar());
        long result = super.insert(Player.TABLE_NAME, values);
        if(result>0)
        	player.setPlayerId((int)result);
        return result;
    }


    public long delete(Player player) {
    	ContentValues contentValues = new ContentValues();
    	contentValues.put(Player.PLAYER_ID, player.getPlayerId());
        long count = super.delete(Player.TABLE_NAME, contentValues);
        return count;
    }
    
    public ArrayList<Player> getAll(){
    	Cursor c = super.getAll(Player.TABLE_NAME, Player.DEFAULT_SORT_ORDER);
    	ArrayList<Player> array = new ArrayList<Player>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Player(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    public ArrayList<Player> getPlayerWithMatchId(int matchId){
        ContentValues values = new ContentValues();
        values.put(Record.TABLE_NAME+"."+Record.MATCH_ID, matchId);
        values.put(Player.TABLE_NAME+"."+Player.PLAYER_ID,Record.TABLE_NAME+"."+Record.PLAYER_ID);
        
        Cursor c = super.find(new String[]{Record.TABLE_NAME,Player.TABLE_NAME}, values,Player.DEFAULT_SORT_ORDER);
    	ArrayList<Player> array = new ArrayList<Player>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Player(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    public Player find(final int playerId) {
        ContentValues values = new ContentValues();
        values.put(Player.PLAYER_ID, playerId);
        Cursor c = super.find(Player.TABLE_NAME, values,Player.DEFAULT_SORT_ORDER);
        Player player = null;
        if (c != null) {
            try {
            	player = new Player(c);
            } catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
                c.close();
            }
        }
        return player;
    }
    
    public boolean update(Player player){
    	boolean flag = true;
        ContentValues values = new ContentValues();
        values.put(Player.PLAYER_ID, player.getPlayerId());
        values.put(Player.NAME, player.getName());
        values.put(Player.AVATAR, player.getAvatar());
    	flag = super.update(Player.TABLE_NAME, values, Player.PLAYER_ID + "=?", new String[]{String.valueOf(player.getPlayerId())})>0;
    	return flag;
    }

}
